package view;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import controller.Controller;

public class InventoryPanel extends GamePanel {
	private static final long serialVersionUID = 1L;
	private JButton[] buttons;
	private int numPrevDrawn;
	private long prevTotalQuantity;
	private MouseListener unitClick;
	private final int numButtons = 10;

	public InventoryPanel(Stage parent, Controller controller,  int w, int h) {
		super(parent, controller, w, h);
	}

	protected void init() {
		unitClick = new MouseListener(){
			public void mouseClicked(MouseEvent e) {
				controller.clickEntity(((JButton)e.getSource()).getName());
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		};
		setLayout(new GridLayout(0, 5));
		setBackground(Color.black);
		buttons = new JButton[numButtons];
		for(int i = 0; i < numButtons; i++){
			buttons[i] = new JButton();
			buttons[i].setVerticalTextPosition(SwingConstants.TOP);
			buttons[i].setHorizontalTextPosition(SwingConstants.SOUTH_EAST);
			buttons[i].setVisible(false);
			add(buttons[i]);
		}
		numPrevDrawn = 0;
		prevTotalQuantity = 0;
		inventoryItemButtons();
	}

	private void inventoryItemButtons() {
		long totalItems = 0;
		for(int i = 0; i < mr.getInventorySize(); i++){
			totalItems += mr.getInventoryItemQuantity(i);
		}
		if(numPrevDrawn != mr.getInventorySize() || totalItems != prevTotalQuantity){
			for(int i = 0; i < numButtons; i++){
				buttons[i].removeMouseListener(unitClick);
			}
			prevTotalQuantity = totalItems;
			numPrevDrawn = mr.getInventorySize();
			for (int i = 0; mr.getInventorySize() > i; i++){
				String path = null;
				if(!mr.entitySelected()){
					switch(mr.getInventoryItemID(i)) {
					case 1:// Player
						path = "/resource/Player.png";
						break;
					case 100:// Tipi
						path = "/resource/Inventory/Tipi.png";
						break;
					default:
						path = "/resource/Inventory.png";
						break;
					}

					buttons[i].setName(mr.getUnitGUID(i));
					buttons[i].setIcon(new ImageIcon(getClass().getResource(path)));
					buttons[i].setVisible(true);
					buttons[i].addMouseListener(unitClick);
				}else{
					String amount = Long.toString(mr.getInventoryItemQuantity(i));
					switch(mr.getInventoryItemID(i)) {
					case 1:// Stick
						path = "/resource/Inventory/Stick.png";
						break;
					case 2:// Shale
						path = "/resource/Inventory/Shale.png";
						break;
					case 3:// Wood
						path = "/resource/Inventory/Wood.png";
						break;
					case 4:// Iron
						path = "/resource/Inventory/Iron.png";
						break;
					case 5:// Clay
						path = "/resource/Inventory.png";
						break;
					case 6:// Stone
						path = "/resource/Inventory/Stone.png";
						break;
					case 50:// Shale Axe
						path = "/resource/Inventory/ShaleAxe.png";
						break;
					case 51:// Iron Axe
						path = "/resource/Inventory.png";
						break;
					case 52:// Iron Pick
						path = "/resource/Inventory.png";
						break;
					case 100:// Tipi
						path = "/resource/Inventory/Tipi.png";
						break;
					default:
						path = "/resource/Inventory.png";
						break;
					}

					buttons[i].setName(Integer.toString(mr.getInventoryItemID(i)));
					buttons[i].setIcon(new ImageIcon(getClass().getResource(path)));
					if(mr.getInventoryItemQuantity(i) != -1)
						buttons[i].setVisible(true);
					else buttons[i].setVisible(false);
					buttons[i].setText(amount);
					buttons[i].addMouseListener(unitClick);
				}
			}
			for(int i = numPrevDrawn; i < numButtons; i++){
				buttons[i].setVisible(false);
			}
			repaint();
		}
	}

	@Override
	protected void update() {
		inventoryItemButtons();
	}
}
